package cn.app.BaseCode.Controller;

import cn.app.BaseCode.service.BaseCrudService;
import cn.app.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/api/{entityName}")
public class BaseCrudController<S extends BaseCrudService<T>, T> {

    @Autowired
    protected S service;

    @GetMapping("/{id}")
    public R<T> getById(@PathVariable Serializable id) {
        return R.ok(service.getById(id));
    }

    @GetMapping
    public R<List<T>> list() {
        return R.ok(service.list());
    }

    @PostMapping
    public R<Boolean> save(@RequestBody T entity) {
        return R.ok(service.save(entity));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody T entity) {
        return R.ok(service.update(entity));
    }

    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Serializable id) {
        return R.ok(service.removeById(id));
    }



}
